package Test7;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.*;

public class FileSymstem {
    private String commond;
    private String src;
    private String des;

    public FileSymstem(){
        this.commond=null;
        this.src=null;
        this.des=null;
    }

    public FileSymstem(String commond,String src){
        this.commond=commond;
        this.src=src;
    }

    public FileSymstem(String commond,String src,String des){
        this.commond=commond;
        this.src=src;
        this.des=des;
    }

    public void achieveLS(String commond,String src){
        if(commond.equals("ls")&&src!=null){
            File f=new File(src);
            String[] list=f.list();
            File[] L=f.listFiles();
            int i=0;
            for(i=0;i<list.length;i++){
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long ret=L[i].lastModified();
                System.out.print(sdf.format(ret)+"  ");
                if(L[i].isDirectory()){
                    System.out.print("<DIR>  ");
                }
                else{
                    System.out.print("       ");
                }
                if(L[i].isFile()){
                    System.out.printf("%12d  ",L[i].length());
                }
                else{
                    System.out.print("              ");
                }
                System.out.println(L[i].getName());
            }
        }
    }

    public boolean copyFile(String commond,String src,String des)throws Exception{
        if(commond.equals("cp")&&src!=null&&des!=null){
            File f=new File(src);
            if(f.exists()){
                FileInputStream fis =new FileInputStream(src);
                FileOutputStream fos=new FileOutputStream(des);
                byte[] buff=new byte[1024];
                int i=fis.read(buff);
                while(i!=-1){
                    fos.write(buff,0,i);
                    i=fis.read();
                }
                fos.flush();
                fos.close();
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public boolean moveFile(String commond,String src,String des)throws Exception {
        if (commond.equals("mv") && src != null && des != null) {
            File f=new File(src);
            if(f.exists()){
                FileInputStream fis =new FileInputStream(src);
                FileOutputStream fos=new FileOutputStream(des,true);
                byte[] buff=new byte[fis.available()];
                fis.read(buff);
                fos.write(buff);
                fos.flush();
                fos.close();
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public boolean deleteFile(String commond,String src)throws Exception {
        if (commond.equals("rm") && src != null) {
            File f=new File(src);
            if(f.exists()&&f.isFile()){
                f.delete();
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public boolean makeDir(String commond,String src) {
        if (commond.equals("md") && src != null) {
            File f=new File(src);
            if(!f.exists()){
                f.mkdir();
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public boolean deleteDir(String commond,String src) {
        if (commond.equals("rd") && src != null) {
            File f=new File(src);
            if(f.exists()&&f.isDirectory()){
                f.delete();
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public void achieveCat(String commond,String src) throws Exception{
        if (commond.equals("cat") && src != null) {
            if(new File(src).exists()){
                FileReader fr=new FileReader(src);
                int i=fr.read();
                while(i!=-1){
                    System.out.print((char)i);
                    i=fr.read();
                }
            }
        }
    }

    //该方法是对一些功能的测试
    public static void main(String[] args) throws Exception{
        /*File f1=new File("c://");
        File[] arr=f1.listFiles();
        String[] arr1=f1.list();
        for(File tmp:arr){
            System.out.print(tmp+" ");
        }
        System.out.println();
        for(String tp:arr1){
            System.out.print(tp+" ");
        }

        File f2=new File("c://AppData");
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatedData=sdf1.format(new Date(f2.lastModified()));
        System.out.println(formatedData);

        File f3=new File("c://");
        String[] list=f3.list();
        File[] L=f3.listFiles();
        int i=0;
        for(i=0;i<list.length;i++){
            SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long ret=L[i].lastModified();
            System.out.println(sdf2.format(ret));
        }

        File f=new File("c://");
        File[] L1=f.listFiles();
        for(i=0;i<L1.length;i++){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long ret=L1[i].lastModified();
            System.out.print(sdf.format(ret)+"  ");
            if(L1[i].isDirectory()){
                System.out.print("<DIR>  ");
            }
            else{
                System.out.print("       ");
            }
            if(L1[i].isFile()){
                System.out.printf("%12d  ",L1[i].length());
            }
            else{
                System.out.print("              ");
            }
            System.out.println(L1[i].getName());
        }

        FileReader fr=new FileReader("E://a.txt");
        i=fr.read();
        while(i!=-1){
            System.out.println((char)i);
            i=fr.read();
        }
        fr.close();

        FileSymstem fs1=new FileSymstem("rm","E://a.txt");
        fs1.deleteFile("rm","E://a.txt");

        FileSymstem fs2=new FileSymstem();
        fs2.makeDir("md","E://a");*/
        FileSymstem fs=new FileSymstem();
        fs.makeDir("md","E://a");

    }
}
